﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SqliteWrapper;
using SqliteWrapper.Query;
using System.Data.SQLite;

namespace SqliteWrapper.Procedures
{
    public class Proc_RateBook : IProcedure
    {
        public override SqliteError doProcedure(object DataSource, string DBPath)
        {
            UserRateData rate = DataSource as UserRateData;
            SqliteError error = new SqliteError() { ErrorText = null, IsProcessSuccess = true };
            SqliteConnectionManager manager = SqliteConnectionManager.Create(DBPath);
            using (SQLiteWriteLock locker = new SQLiteWriteLock(manager.ConnectionString))
            {
                using (SQLiteConnection conn = manager.GetSqliteConnection())
                {
                    conn.Open();
                    using (SQLiteCommand cmd = conn.CreateCommand())
                    {
                        SQLiteParameter param = new SQLiteParameter("UserId", System.Data.DbType.UInt64);
                        param.Value = rate.UserId;
                        QueryBuilder q_builder = new QueryBuilder("Rates", "ID")
                        {
                            StartIndex = null,
                            Length = null,
                            StartTime = null,
                            EndTime = null,
                            OrderBy = null,
                            Parameters = new List<SQLiteParameter>() {param}
                        };
                        cmd.CommandText = q_builder.BuildSelect();
                        cmd.CommandType = System.Data.CommandType.Text;
                        try
                        {
                            var id = cmd.ExecuteScalar();
                            if (id != null)
                            {
                                error.ErrorText = "对不起，您已经参与过评分，每位用户只允许投票一次";
                                error.IsProcessSuccess = false;
                            }
                            else
                            {
                                using (SQLiteCommand cmd2 = conn.CreateCommand())
                                {
                                    cmd2.CommandText = "insert into rates(BookId,Good,UserId) values(@BookId,@Good,@UserId)";
                                    cmd2.CommandType = System.Data.CommandType.Text;
                                    cmd2.Parameters.Add("@BookId", System.Data.DbType.UInt64).Value = rate.BookId;
                                    cmd2.Parameters.Add("@Good", System.Data.DbType.Boolean).Value = rate.IsGood;
                                    cmd2.Parameters.Add("@UserId", System.Data.DbType.UInt64).Value = rate.UserId;

                                    try
                                    {
                                        cmd2.ExecuteNonQuery();
                                    }
                                    catch (Exception ex)
                                    {
                                        error.ErrorText = ex.Message;
                                        error.IsProcessSuccess = false;
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            error.ErrorText = ex.Message;
                            error.IsProcessSuccess = false;
                        }
                    }
                }
            }
            return error;
        }
    }
}
